package yxy.game.pm2.command.mail;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.MailMessage;
import yxy.game.pm2.module.mail.MailHandle;

/** 邮件 邮件列表 */
@MessageType(proto = Command.MailList, classtype = MailMessage.MailListRequest.class)
public class CMD_MailList extends Command<MailMessage.MailListRequest> {

	@Override
	public void execute(MessageSource messageSource, MailMessage.MailListRequest request) throws RedisException {
		String playerCode = messageSource.getTag();

		MailMessage.MailListResponse.Builder response = MailMessage.MailListResponse.newBuilder();

		MailHandle mailHandle = new MailHandle();
		{
			// XXX 测试用
//			ArrayList<Currency> rewards = new ArrayList<>();
//			rewards.add(Currency.create(CurrencyType.CT_元宝, 0, RandomUtility.nextInt(10, 30)));
//			mailHandle.sendMail(playerCode, MailTypeEnum.Mail, "测试测试", "真的是测试喔~~", rewards);
		}

		response.addAllMails(mailHandle.packMails(playerCode));

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, MailMessage.MailListResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}